package com.smap.proj;


/**
 * This is ported from gdal2tiles::GlobalGeodetic Python source code
 * @author Shen
 *
 */
public class GlobalGeodetic {
	private final int tileSize;

	public GlobalGeodetic() {
		tileSize = GlobalMercator.TILE_SIZE;
	}

	public int[] LatLonToPixels(double lat, double lon, int zoom) {
		double res = 180 / 256.0 / Math.pow(2, zoom);
		int px = (int) ((180 + lat) / res);
		int py = (int) ((90 + lon) / res);
		return new int[] { px, py };
	}

	public int[] PixelsToTile(int px, int py) {
		int tx = (int) (Math.ceil(px / ((float) tileSize) - 1));
		int ty = (int) (Math.ceil(py / ((float) tileSize) - 1));
		return new int[] { tx, ty };
	}

	public double Resolution(int zoom) {
		return 180 / 256.0 / Math.pow(2, zoom);
	}

	public int[] TileBounds(int tx, int ty, int zoom) {
		double res = 180 / 256.0 / Math.pow(2, zoom);
		return new int[] { (int) (tx * 256 * res - 180),
				(int) (ty * 256 * res - 90),
				(int) ((tx + 1) * 256 * res - 180),
				(int) ((ty + 1) * 256 * res - 90) };
	}

}
